package com.ly.cloud.service.impl;

import com.baomidou.mybatisplus.plugins.Page;
import com.ly.cloud.dto.DepartmentDto;
import com.ly.cloud.entity.DepartmentPo;
import com.ly.cloud.exception.CloudException;
import com.ly.cloud.mapper.DepartmentMapper;
import com.ly.cloud.service.DepartmentService;
import com.ly.cloud.utils.BeanCopyUtils;
import com.ly.cloud.vo.DepartmentVo;
import com.ly.cloud.vo.DeptTypeSelectVo;
import com.ly.cloud.vo.TeachDepVo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @Description:
 * @Author: pengshiqng
 * @Date: Create in 2018/12/12
 */
@Service
public class DepartmentServiceImpl implements DepartmentService {

    @Autowired
    private DepartmentMapper departmentPoMapper;


    @Override
    public Page<DepartmentVo> selectPage(Page<DepartmentVo> page , String id, DepartmentDto dto) {
        try {
            return page.setRecords(departmentPoMapper.selectPage(page, id, dto));
        }catch (Exception e){
            throw new CloudException("查询异常，请重试!", e);
        }
    }

    @Override
    public int deleteById(String id) {
        try {
            return departmentPoMapper.deleteById(id);
        } catch (Exception e){
            e.printStackTrace();
            throw new CloudException("删除异常，请重试!", e);
        }
    }

    @Override
    public int deleteByIds(List<String> ids) {
        try {
            return departmentPoMapper.deleteBatchIds(ids);
        } catch (Exception e){
            e.printStackTrace();
            throw new CloudException("批量删除异常，请重试!", e);
        }
    }

    @Override
    public int insert(String loginUserName ,DepartmentDto dto) {
        try {
            //把dto对象转po对象
            DepartmentPo po = BeanCopyUtils.copyBeanProperties(dto, DepartmentPo.class);
            po.setCzr(loginUserName);
            po.setCzsj(new Date());
            return departmentPoMapper.insert(po);
        } catch (Exception e){
            e.printStackTrace();
            throw new CloudException("新增失败!", e);
        }
    }

    @Override
    public int update(String loginUserName , DepartmentDto dto) {
        try {
            //把dto对象转po对象
            DepartmentPo po = BeanCopyUtils.copyBeanProperties(dto, DepartmentPo.class);
            po.setXgr(loginUserName);
            po.setXgsj(new Date());
            return departmentPoMapper.updateById(po);
        } catch (Exception e){
            e.printStackTrace();
            throw new CloudException("更新失败!", e);
        }
    }

    @Override
    public List<TeachDepVo> findAllDepartment(String id) {
        try {
            return departmentPoMapper.findAllDepartment(id);
        } catch (Exception e){
            e.printStackTrace();
            throw new CloudException("查询失败!", e);
        }
    }

    /**
     * 查询教学机构（院系下拉）
     * 教学机构代码值固定：01，参考代码集的单位类别码
     * @author caihuihai 2018-12-20
     */
	@Override
	public List<TeachDepVo> findTeachDep() {
		try {
            return departmentPoMapper.findTeachDep();
        } catch (Exception e){
            e.printStackTrace();
            throw new CloudException("查询失败!", e);
        }
	}

	/**
	* @Description: 查询所有单位类别(下拉通用)
	* @Author: pengshiqing
	* @CreateDate: 2018/12/22
	*/
    @Override
    public List<DeptTypeSelectVo> findDepType() {
        try {
            return departmentPoMapper.findDepType();
        } catch (Exception e){
            e.printStackTrace();
            throw new CloudException("查询失败!", e);
        }
    }

    /**
    * @Description: 新增部门时，查询部门编号是否存在
    * @Author: pengshiqing
    * @CreateDate: 2018/12/22
    */
    @Override
    public Boolean checkedDepID(String depId) {
        try {
            int result = departmentPoMapper.checkedDepID(depId);
            if (result == 0 ){
                return false;
            }else{
                return true;
            }
        } catch (Exception e){
            e.printStackTrace();
            throw new CloudException("查询失败!", e);
        }
    }
}
